﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;

public partial class Controls_Activity_MemberListDetail : System.Web.UI.UserControl
{
    private int m_ActivityId = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["activityId"] == null)
            return;
        int activityId = -1;
        if (!int.TryParse(Request["activityId"], out activityId))
            return;
        m_ActivityId = activityId;
        GridView1.DataSource = ActivityRules.Instance.GetActivityMemberListDetail(activityId, IsCandidate);
        if (!ActivityRules.Instance.IsCurrentUserIsActivityPromoter(m_ActivityId))
        {
            GridView1.Columns[0].ItemStyle.CssClass = "activity_itemPost_memberwidth100";
            GridView1.Columns[1].ItemStyle.CssClass = "activity_itemPost_memberwidth100";
            GridView1.Columns[2].Visible = false;
            GridView1.Columns[3].Visible = false;
            GridView1.Columns[4].Visible = false;
            GridView1.Columns[5].Visible = false;
        }
        else
        {
            if (!IsCandidate)
            {
                GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
            }
        }
        if (IsCandidate)
        {
            GridView1.EmptyDataText = "没有待批准成员";
        }
        else
        {
            GridView1.EmptyDataText = "没有已批准成员";
        }
        if (!IsPostBack)
        {
            GridView1.DataBind();
        }
    }

    public bool IsCandidate { get; set; }

    protected void lbAgree_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        string[] id = btn.CommandArgument.Split('|');
        string userId = id[0];
        int activityId = Convert.ToInt32(id[1]);
        ActivityRules.Instance.UpdateUserActivityState(activityId, userId, "joined");
        GridView1.DataBind();
        ActivityRules.Instance.SendAcceptMessage(m_ActivityId, userId);
        Response.Redirect("~/Activity/Detail.aspx?show=member&activityId=" + m_ActivityId);

    }
    protected void lbReject_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        string[] id = btn.CommandArgument.Split('|');
        string userId = id[0];
        int activityId = Convert.ToInt32(id[1]);
        ActivityRules.Instance.QuitActivity(userId, activityId);
        GridView1.DataBind();
        ActivityRules.Instance.SendRejectMessage(m_ActivityId, userId);
        Response.Redirect("~/Activity/Detail.aspx?show=member&activityId=" + m_ActivityId);
    }

    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lbuserId = (Label)e.Row.Cells[6].FindControl("lb_userId");
            string userid = lbuserId.Text;
            if (!IsCandidate && ActivityRules.Instance.IsActivityPromoter(m_ActivityId, userid))
            {
                e.Row.Cells[5].FindControl("lbReject").Visible = false;
            }

            e.Row.Cells[5].FindControl("lbAgree").Visible = false;
        }
    }
}
